<?php

/**
 * Controller_department
 */

class Controller_department extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->staff    		= YOU::loadModel('staff');
		$this->model->appoint    	= YOU::loadModel('appoint');

	}
	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}
	/** ---------------------------------部门设置------------------------------------- **/
	/** 部门列表 **/
	public function actionDepartmentList()
	{
		
		// 所有老师
		$staff_lists = $this->model->staff->getAll();
		/*//print_R($staff_lists);
			foreach ($staff_lists AS $key => $value){
				print_R($value['staff_id']);echo "\n";
				$appoint_data =$this->model->appoint->getDselRow($value['staff_id'],$department_id);
				if (!empty($appoint_data)) {
					echo 1;
					$value['kong']=1;
				}
			}*/
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_department`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$department_lists = $this->db->fetchAll("SELECT * FROM `tbl_department` ORDER BY department_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($department_lists as $key => $value){
			// 普通员工
			$row  = $this->model->appoint->getDsel($value['department_id']);
			foreach ($row as $k => $v){
				// 员工姓名
				$rows  = $this->model->staff->getinfo($v['staff_id']);
				$department_lists[$key]['staff_name'][$v['staff_id']] = $rows['staff_name'];
			}
		}
		// 添加
		if ($_POST) {
			$department_id     		= $this->post->department_id;
			$staff_id     			= $this->post->staff_id;
			
			// 先删除 在存入
			if (!empty($department_id)) {
				$this->db->query("DELETE  FROM `tbl_appoint` WHERE appoint_common='1' AND department_id='{$department_id}'");
			}
			// 查找原始人员记录
			//$list  = $this->model->appoint->getDsel($department_id);

			foreach ($staff_id as $key => $value){

				// 查找任职表
				//$appoint_data =$this->model->appoint->getDselRow($value,$department_id);
				// 写入数据库
				$info = array(

				"department_id"      	=> $department_id,
				"staff_id"      		=> $value,
				"appoint_common"      	=> 1,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);

				$this->db->do_insert("tbl_appoint", $info);

			}

			Helper_Client::location("添加成员成功", "index.php?a=department.departmentlist");
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("department_lists", $department_lists);
		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->display('department/department.list.tpl.php');
	}
	public function actionStaffAdd()
	{
		$this->tpl->display('department/department.staff.tpl.php');
	}
	/** 部门添加 **/
	public function actionDepartmentAdd()
	{
		if ($_POST){

			$department_name     	= $this->post->department_name;

			// 写入数据库
			$info = array(

			"department_name"      	=> $department_name,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_department", $info);

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=department.departmentlist");
		}

		$this->tpl->display('department/department.add.tpl.php');
	}
	/** 部门修改 **/
	public function actionDepartmentUpdate()
	{

		$department_id   = (int)$this->get->department_id;
		$department_data = $this->db->fetchRow("SELECT * FROM `tbl_department` WHERE `department_id`='{$department_id}'");

		if (empty($department_data)){
			Helper_Client::alert("部门不存在");
		}

		if ($_POST){

			$department_name        = $this->post->department_name;

			// 写入数据库
			$info = array(

			"department_name"     	=> $department_name,
			"create_time"           => date("Y-m-d H:i:s"),
			);
			$this->db->do_update("tbl_department", $info, "department_id='{$department_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=department.departmentlist");
		}

		$this->tpl->assign("department_data", $department_data);
		$this->tpl->display('department/department.update.tpl.php');
	}
	/** 部门删除 **/
	public function actionDepartmentDel()
	{
		$department_id = $this->get->department_id;
		if ($department_id !=1) {
			$this->db->query("DELETE  FROM `tbl_department` WHERE department_id='{$department_id}'");

			// 跳转
			Helper_Client::location("删除成功", "index.php?a=department.departmentlist");
		}else {
			Helper_Client::location("校长室不能删除", "index.php?a=department.departmentlist");
		}

	}
	/** -------------------------------- 上级部门设置部门 ------------------------------- **/
	/** 上级部门列表 **/
	public function actionUpDepartmentList()
	{
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_department_up`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$up_lists = $this->db->fetchAll("SELECT * FROM `tbl_department_up` ORDER BY up_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("up_lists", $up_lists);
		$this->tpl->display('department/department.up.list.tpl.php');
	}
	/** 上级部门添加 **/
	public function actionUpDepartmentAdd()
	{
		if ($_POST){

			$up_name     	= $this->post->up_name;

			// 写入数据库
			$info = array(

			"up_name"      			=> $up_name,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_department_up", $info);

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=department.updepartmentlist");
		}

		$this->tpl->display('department/department.up.add.tpl.php');
	}
	/** 上级部门修改 **/
	public function actionUpDepartmentUpdate()
	{

		$up_id   		 = (int)$this->get->up_id;
		$up_data = $this->db->fetchRow("SELECT * FROM `tbl_department_up` WHERE `up_id`='{$up_id}'");

		if (empty($up_data)){
			Helper_Client::alert("部门不存在");
		}

		if ($_POST){

			$up_name        		= $this->post->up_name;

			// 写入数据库
			$info = array(

			"up_name"     			=> $up_name,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",
			);
			$this->db->do_update("tbl_department_up", $info, "up_id='{$up_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=department.updepartmentlist");
		}

		$this->tpl->assign("up_data", $up_data);
		$this->tpl->display('department/department.up.update.tpl.php');
	}
	/** 上级部门删除 **/
	public function actionUpDepartmentDel()
	{
		$up_id = $this->get->up_id;
		$this->db->query("DELETE  FROM `tbl_department_up` WHERE up_id='{$up_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=department.updepartmentlist");
	}


}

?>